<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>东财个股人气榜</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">				
			//p {margin:5px}
			//ul {margin:1px}			
			//h4 {margin:1px}	
			.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}			
		</style>		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script>
			window.onload = function() {
			getSource1();
			changeStyle();
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					getSource1();					
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
				//time_range("9:00","15:15")
				getSource1();				
				},
				60000);
			})
						
			function changeStyle() {
				<!-- var obj1 = document.getElementById("towrite");				 -->
				<!-- obj1.style.left= "0px"; 				 -->
				<!-- obj1.style.width= "480px";	 -->
				<!-- obj1.style.height= "620px";		 -->
				<!-- obj1.style.fontSize="12px";				  			  -->
				var obj = document.getElementById("towriter");				
				obj.style.left= "0px"; 				
				obj.style.width= "640px";	
				obj.style.height= "630px";	
				
				var obj1 = document.getElementById("towrite1");				
				obj1.style.width= "480px";	//左侧宽度
				obj1.style.fontSize="12px";	//左侧字体

				var obj2 = document.getElementById("towrite2");
				obj2.style.left= "320px";   //右侧左边位置
				obj2.style.width= "300px";  //右侧宽度
				obj2.style.height= "550px";  //右侧高度
				obj2.style.fontSize="12px";	//右侧字体
				obj2.style.marginTop= "30px"; 
				//obj2.style.whiteSpace= "normal";
			}	
						
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
			return new Date(parseInt(nS) * 1000).Format("hh:mm:ss")}						
									
			function createXmlHttp() {
				if (window.XMLHttpRequest) {
				xmlHttp = new XMLHttpRequest();
				} else {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}				
			}
			
			function changeCss(obj,id){				
				var alist = document.getElementById(id).getElementsByTagName("a");					
				for(var i = 0; i < alist.length; i++){				
					if(alist[i] == undefined){
						break;
					}
					if(alist[i].id != "rd"){
					alist[i].style.color = "#d2d2d3"; //给所有a标签赋原色
					}
				}
				obj.style.color = "#00FFFF"; //令当前标签高亮				
			}
			
			function getSource1() {	    							
				var url = "https://emappdata.eastmoney.com/stockrank/getAllCurrentList"; 	                			
				createXmlHttp();
				var array = {"appId":"appId01","globalId":"786e4c21-70dc-435a-93bb-38","pageNo":1,"pageSize":100}
				xmlHttp.onreadystatechange = writeSource1;
				xmlHttp.open("POST", url, true);
				xmlHttp.setRequestHeader("Content-Type","application/json");
				xmlHttp.withCredentials = true
				//xmlHttp.send(null);
				xmlHttp.send(JSON.stringify(array)) ; 
			}
								
			var dms = "";
			var dmr = "";
			function writeSource1() { 
				if (xmlHttp.readyState == 4) {         
					var data = xmlHttp.responseText	
					//document.write(data);
					 ss = data.split('},{');				
					for (i = 0; i < ss.length; i++)
					{
					//alert(ss[i]);
						dms += ss[i].match(/\"sc\":\"(\S*)\",/)[1].replace("SH","1.").replace("SZ","0.") + ",";
						dmr += ss[i].match(/\"sc\":\"(\S*)\",/)[1].replace("SH","").replace("SZ","") + ",";
					}
					//dms = dms.substring(0,dms.length - 1);
					//document.write(dms);
					getSource2();
					getSource3();
				}
			}
			
			function getSource2() {	    							
				var url = "https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&np=3&ut=a79f54e3d4c8d44e494efb8f748db291&invt=2&secids=" + dms + "&fields=f2,f3,f12,f14"; 	                							
				createXmlHttp();				
				xmlHttp.onreadystatechange = writeSource2;
				xmlHttp.open("GET", url, true);				
				//xmlHttp.withCredentials = true
				xmlHttp.send(null);				
			}
			
			function getSource3() {	    							
				var url = "https://vipmoney.eastmoney.com/collectapi/ranking/GubaHotTopicNew?code=" + dmr;
				if (window.XMLHttpRequest) {
					xmlHttp3 = new XMLHttpRequest();
				}else{
					xmlHttp3 = new ActiveXObject("Microsoft.XMLHTTP");
				}						
				xmlHttp3.onreadystatechange = writeSource2;
				xmlHttp3.open("GET", url, true);				
				xmlHttp3.send(null);				
			}
			
			function writeSource2() { 
			if (xmlHttp.readyState == 4 && xmlHttp3.readyState == 4) {         
				var data = xmlHttp.responseText	
				data = data.replace(/ /g,"").match(/(\S*)}\]}}/)[1]
				var data3 = xmlHttp3.responseText
				//data3 = data3.replace(/ /g,"");
				//document.write(data3);
				
				var aa = "";
				var ss = data.split("},{");
				for (i = 0; i < ss.length; i++){				
					var code = ss[i].match(/"f12":"(\S*)/)[1].split('"')[0];
					//alert('"' + code + '":[{')
					var m = ss[i]
					var cc = '"htid":"","name":""';
					if(data3.indexOf('"' + code + '":[]') == -1 && data3.indexOf('"' + code + '":null') == -1){
						var bb = data3.split('"' + code + '":[{')[1];					
						cc = bb.split(',"num"')[0];
					}
					//alert(cc)
					aa += m + "," + cc + "},{";
					//alert(data)
				}
				aa = aa + "}]}}"
				aa = aa.replace("},{}]}}","}]}}");
				//aa = aa.replace("}]}}},{","}]}}");
				//document.write(aa);
				//var json = JSON.parse(aa);
				
				var transform_aa = [           
				{"tag":"p", children:function(){return(json2html.transform(this.data, transform_bb));}}
				];
				var transform_bb = [           
				{"tag":"p", children:function(){return(json2html.transform(this.diff, transform_cc));}}
				];
				
				m = 0;
				var transform_cc= 				
				{"tag":"ul", children:[	
				{"tag":"li", children:function(){
					m++;
					var rd = "";
					if(this.name != ""){
						//var rd = "<font color=#FFFF00>热点：" + this.name + "</font>";
						var rd = "<br><a href='javascript:void(0)' id='rd' style='color:#FFFF00;' onclick='update(\"" + this.htid + "\",\"" + this.name + "\");'>热点：" + this.name + "</a>"
					}
					return(
					"<a href=http://www.treeid/code_" +
						this.f12 + " onclick='changeCss(this,\"towrite1\")'>" + m + ". " + 						
						this.f12 + "&nbsp;&nbsp;" + 
						this.f14 + "&nbsp;&nbsp;" +
						"现价：" + this.f2 + "&nbsp;&nbsp;" +
						"涨幅：" + this.f3 + "%" +
						"</a>" + rd
					);
				}}			
				//{"tag": "li", "html":"<a href='http://www.treeid/code_${f12}' >${f12}&nbsp;&nbsp;${f14}&nbsp;涨幅：${f3}% </a>"}, 							
				]};  
				
				var html = json2html.transform(aa, transform_aa);		
				div = document.getElementById('towrite1');
				var myDate = new Date();
				div.innerHTML = '<h4>东财个股人气榜&nbsp;&nbsp;&nbsp;&nbsp;' + myDate.Format("hh:mm:ss") + '</h4>' + html;
				}
			}
			
			function update(id,rd) {
				//alert(id)
				div = document.getElementById('towrite2');										
				var url = "http://gubatopic.eastmoney.com/interface/GetData.aspx"; 				
				div.innerHTML = '<h4>热点：' + rd  + '</h4>';				
				xhr = new XMLHttpRequest();				
				var array = "param=htid=" + id + "&path=newtopic/api/Topic/TopicDetailsRead";						
				xhr.open("POST", url, false);  //同步方式				
				xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");				
				xhr.send(array);				
				var data = xhr.responseText	
				data = data.replace(/\s+/g,"").replace(/<br>/g,"").replace(/[\r|\n]/g,"");
				//document.write(data);
				var rdjd = data.match(/"lead":"(\S*)","participantCount/)[1];
				div.innerHTML += "<ul style='color:#FAFAD2;font-size:14px;margin-left:-5px;white-space:normal;'>" + rdjd + "</ul>";
				
				var dms = "";
				var aa = data.match(/"StockListNew":\[(\S*)/)[1].split("\]")[0].replace(/"/g,"");
				var bb = data.match(/"StockFixListNew":\[(\S*)/)[1].split("\]")[0].replace(/"/g,"");
				
				var cc = aa.split(",");
				for (i = 0; i < cc.length; i++){
					var dm = cc[i];
					if(dms.indexOf(dm) == -1 && dm.indexOf("90_BK") == -1){
						dms += dm + ",";
					}
				}
				var dd = bb.split(",");
				for (i = 0; i < dd.length; i++){
					var dm = dd[i];
					if(dms.indexOf(dm) == -1 && dm.indexOf("90_BK") == -1){
						dms += dm + ",";
					}
				}
				dms = dms.replace(/_/g,".");
				//alert(dms)
				
				var url1 = "https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&np=3&ut=a79f54e3d4c8d44e494efb8f748db291&invt=2&secids=" + dms + "&fields=f2,f3,f12,f14"; 	                							
				if (window.XMLHttpRequest) {
					xmlHttp4 = new XMLHttpRequest();				
				} else {
					xmlHttp4 = new ActiveXObject("Microsoft.XMLHTTP");				
				}
				xmlHttp4.open("GET", url1, false);  //同步方式
				xmlHttp4.send(null);				
			    
				var data1 = xmlHttp4.responseText
				data1 = data1.replace(/ /g,"");
				data1 = data1.match(/"diff":(\S*)}]}}/)[1] + "}]";
				//document.write(data1);
				var result = JSON.parse(data1); //JSON字符串转换为对象				
				function up(a,b){
					
					return b.f3-a.f3
				}					
				result.sort(up);
				result = JSON.stringify(result);		
				data1 = '{"data":' + result + "}"
				var transform_aa = [           
				{"tag":"p", children:function(){return(json2html.transform(this.data, transform_bb));}}
				];
				
				m = 0;
				var transform_bb= 				
				{"tag":"ul", children:[	
				{"tag":"li", children:function(){
					m++;					
					return(
					"<a href=http://www.treeid/code_" +
						this.f12 + " onclick='changeCss(this,\"towrite2\")'>" + m + ". " + 						
						<!-- this.f12 + "&nbsp;&nbsp;" +  -->
						this.f14 + "&nbsp;&nbsp;" +
						"现价：" + this.f2 + "&nbsp;&nbsp;" +
						"涨幅：" + this.f3 + "%" + "&nbsp;&nbsp;" + 
						"</a>" 					
					);
				}}			
				//{"tag": "li", "html":"<a href='http://www.treeid/code_${f12}' >${f12}&nbsp;&nbsp;${f14}&nbsp;涨幅：${f3}% </a>"}, 							
				]};  
				
				var html = json2html.transform(data1, transform_aa);						
				div.innerHTML += "相关股票：" + html;
			}
			
		</script>
	</head>
	<body>
		<div id="towriter" class="fixedBox">			
			<div id="towrite1" ></div>		
			<div id="towrite2" class="fixedBox" ></div>
		</div>	
	</body>
</html>